<template>
  <div class="app-container">
         <el-row :gutter="15">
      <el-form ref="elForm" :model="formData" :rules="rules" size="medium" label-width="100px"
        label-position="left">
        <el-col :span="13">
          <el-form-item label="赛制名" prop="name">
            <el-input v-model="formData.name" placeholder="请输入赛制名" clearable :style="{width: '100%'}">
            </el-input>
          </el-form-item>
        </el-col>
        <el-col :span="13">
          <el-form-item label="赛制描述" prop="decription">
            <el-input v-model="formData.decription" placeholder="请输入赛制描述" clearable :style="{width: '100%'}">
            </el-input>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item size="large">
            <el-button type="primary" @click="submitForm">提交</el-button>
            <el-button @click="resetForm">重置</el-button>
          </el-form-item>
        </el-col>
      </el-form>
    </el-row>
  </div>
</template>
<script>
import style from '@/api/comp/style'
export default {
  components: {},
  props: [],
  data() {
    return {
      formData: {
        name: undefined,
        decription: undefined,
      },
      styleId:undefined,
      rules: {
        name: [{
          required: true,
          message: '请输入赛制名',
          trigger: 'blur'
        }],
        decription: [{
          required: true,
          message: '请输入赛制描述',
          trigger: 'blur'
        }],
      },
    }
  },
  computed: {},
  watch: {},
  created() {
    this.init();
  },
  mounted() {},
  methods: {
    submitForm() {
      this.$refs['elForm'].validate(valid => {
        // 判断添加还是修改
        if (this.styleId) {
          this.updateEvent();
        } else {
          this.addEvent();
        }
      })
    },
    resetForm() {
      this.$refs['elForm'].resetFields()
    },
    init() {
    console.log("created");
    if (this.$route.params && this.$route.params.id) {
      const id = this.$route.params.id
      this.getInfo(id)
      this.styleId = id
    } else {
      // 为什么没有解决问题
      // create 方法只执行一次
      // 多次路由跳转到同一个界面，在页面中create方法只会执行一次
      // 使用vue监听
      this.formData = {};
    }
    },
    getInfo(id) {
      style.getStyleById(id).
      then((response) => {
        this.formData = response.data.style
      })
      .catch((error) => {
      });
    },
    addEvent() {
      // TODO 提交表单
      style.addStyle(this.formData).
      then((response) => {
      //提示添加成功
        this.$message({
          type: "success",
          message: "添加成功!",
        });
        //回到列表界面 路由跳转 => 重定向
        this.$router.push({ path: "/style/list" });
      })
      .catch((error) => {
        console.log(error)
      });
    },
    updateEvent() {
      // TODO 提交表单
      style.updateStyle(this.formData).
      then((response) => {
      //提示添加成功
        this.$message({
          type: "success",
          message: "修改成功!",
        });
        //回到列表界面 路由跳转 => 重定向
        this.$router.push({ path: "/style/list" });
      })
      .catch((error) => {
        console.log(error)
      }); 
    }
  }
}

</script>
<style>
</style>
